/*******************************************************************************
Author: Jason Robey
Date: 5/15/2025
Purpose: Run  APCC models for single-year single-age analyses
Output: Table 2 descriptives and Table 5 model results 
*******************************************************************************/

cls 
clear 
capture log close

*Define using working directories
global path ""

*raw data directory
global rd "$path/"

*working data directory
global wd "$path/"

*log file directory
global ld "$path/"

*output file directory
global od "$path/"

cd $wd

*Data string to append to log file
local date: display %td_CCYY-NN-DD date(c(current_date), "DMY")
local date_string = subinstr(trim("`date'"), "-" , ".", .)

*Starting log file 
log using "$ld/name.`date_string'.log", replace

ssc install estout

/*******************************************************************************
Analysis of Single Year Age-Year-Cohort Data
*******************************************************************************/

use 2.1.2.merge_year.dta, clear

*Case selection to remain consistent with prior literature 
keep if year<2015 & age<60 

order ahar ahar_lead ahar_avg whar whar_lead whar_avg bhar bhar_lead bhar_avg ///
	anmar apop_per_15_19, after(birth_year)


*Descriptive Stats for Table 2
table (var) if !missing(ahar, anmar, apop_per_15_19) &  year<2015 & age<60, ///
	stat(mean ahar ahar_lead ahar_avg anmar wnmar bnmar apop_per_15_19) ///
	stat(sd ahar ahar_lead ahar_avg anmar wnmar bnmar apop_per_15_19) ///
	stat(count ahar ahar_lead ahar_avg anmar wnmar bnmar apop_per_15_19) ///
	stat(freq) ///
	nformat(%9.1fc percent mean sd) ///
	sformat("%s%%" percent) ///
	sformat("(%s)" sd)	
collect export "$od/table1b.docx", replace

	
***APCC Regression Modeling 
est clear 

***Table 5
foreach dem in a w b {
	foreach var in har har_lead har_avg {
			quietly reg `dem'`var'_log i.year i.age apop_per_15_19 `dem'nmar, robust
			est sto `dem'`var'`typ'
	}
}

*All Models 
#delimit ;
esttab *har*,
	nonumber label obslast constant omit
	title("National APCC Models for Logged Homicide Arrest Rates")
	b(%10.3f) se(%10.3f) ar2(%9.3f) star(* 0.05 ** 0.01 *** 0.001);
#delimit cr

*All Models - Exported in a CSV
#delimit ;
esttab *har* using "$od/Table5.csv", plain replace
	nonumber label obslast constant omit
	title("National APCC Models for Logged Homicide Arrest Rates")
	b(%10.3f) se(%10.3f) ar2(%9.3f) star(* 0.05 ** 0.01 *** 0.001);
#delimit cr


log close 
